package com.my.util;

import java.io.*;

/**
 * @author liangxin
 * @since 08/08/2019
 */
public class FileUtils {




    //纯粹练习熟练java
    /**
     *将其中的某些空格替换成逗号（，），即对于如下格式的文本文件内容：
     * 起始IP 结束IP 地区
     * 61.54.231.245 61.54.231.245 河南省安阳市 新世纪网吧
     * 61.54.231.246 61.54.231.246 河南省安阳市 未知地区
     * 61.54.231.9 61.54.231.247 河南省安阳市 红日网吧
     * 61.54.231.248 61.54.231.248 河南省安阳市 安阳
     *
     *  src_path: D:\98\java\a.txt    goal_path: D:\98\java\b.txt
     */
    public static void testJavaFile1(String src_path, String goal_path) throws IOException {
        BufferedReader src_reader = new BufferedReader(  new FileReader(src_path));
        BufferedWriter goal_Writer = new BufferedWriter(new FileWriter(goal_path));
        String str_temp;
        while ((str_temp = src_reader.readLine()) != null) {
            String str = str_temp.replaceAll("(?<=\\d)\\s+", ",");//空格替换逗号
            String[] array = str.split(",");
            for (int i = 0; i < array.length - 1; i++) { // i : 行
                String[] sub_array = array[i].split("\\.");
                String num = "";
                for (int j = 0; j < sub_array.length; j++) { //首位补零
                    String sub_str = "";
                    if (sub_array[j].length() == 1) {
                        sub_str = "00" + sub_array[j];
                    } else if (sub_array[j].length() == 2) {
                        sub_str = "0" + sub_array[j];
                    } else {
                        sub_str = sub_array[j];
                    }

                    if (3 == j) { //ip 第4位后面加逗号，其他位数后面加英文句号
                        num = num + sub_str + ',';
                    } else {
                        num = num + sub_str + '.';
                    }
                }
                System.out.print(num);
                goal_Writer.write(num);
                if (i % 2 == 1) {
                    System.out.println(array[2]);
                    goal_Writer.write(array[2] + "\r\n");
                }
            }
        }
        goal_Writer.flush();
        src_reader.close();
        goal_Writer.close();
    }

}
